package com.yanqu.road.dao.impl.systemgroup;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.systemgroup.common.ServerWeekData;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ServerWeekDataDaoImpl extends TempDataObjectDao<ServerWeekData> {

    public static String getServerWeekDataSql = "select * from `t_c_new_system_server_week_data` where `weekId`=?";
    public Map<Integer, Map<Long, ServerWeekData>> getServerWeekData(int weekId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Long, ServerWeekData>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getServerWeekDataSql);
            int i = 1;
            call.setInt(i++, weekId);
            rs = call.executeQuery();
            while (rs.next()) {
                ServerWeekData data = parse(rs);
                if(!map.containsKey(data.getSystemId())){
                    map.put(data.getSystemId(), new ConcurrentHashMap<>());
                }
                map.get(data.getSystemId()).put(data.getServerId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateServerWeekDataSql = "update `t_c_new_system_server_week_data` set ability=?,updateTime=? where weekId=? and systemId=? and serverId=?";
    @Override
    protected boolean updateData(ServerWeekData serverWeekData) {
        serverWeekData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, serverWeekData.getAbility().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, serverWeekData.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, serverWeekData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, serverWeekData.getSystemId()));
        para.put(i++, new DbParameter(Types.BIGINT, serverWeekData.getServerId()));
        if (getDb().executeNoneQuery(updateServerWeekDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertServerWeekDataSql = "insert into `t_c_new_system_server_week_data`(weekId,systemId,serverId,ability,updateTime) values (?,?,?,?,?)";
    @Override
    protected boolean addData(ServerWeekData serverWeekData) {
        serverWeekData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, serverWeekData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, serverWeekData.getSystemId()));
        para.put(i++, new DbParameter(Types.BIGINT, serverWeekData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, serverWeekData.getAbility().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, serverWeekData.getUpdateTime()));
        if (getDb().executeNoneQuery(insertServerWeekDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ServerWeekData parseData(ResultSet rs) throws SQLException {
        ServerWeekData serverWeekData = new ServerWeekData();
        serverWeekData.setWeekId(rs.getInt("weekId"));
        serverWeekData.setSystemId(rs.getInt("systemId"));
        serverWeekData.setServerId(rs.getLong("serverId"));
        String ability = rs.getString("ability");
        if(StringUtils.isNullOrEmpty(ability)){
            serverWeekData.setAbility(BigInteger.ZERO);
        }else {
            serverWeekData.setAbility(new BigInteger(ability));
        }
        serverWeekData.setUpdateTime(rs.getInt("updateTime"));

        return serverWeekData;
    }
}
